﻿using EpubFactory.Zhuishu.Model;
using EpubFactory.Zhuishu.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EpubFactory.Zhuishu
{
    public class LibZhuiShu
    {

        public LibZhuiShu()
        {

        }

        /// <summary>
        /// 搜索小说
        /// </summary>
        /// <param name="query">关键词</param>
        /// <param name="start">结果开始Index</param>
        /// <param name="limit">结果数量限制</param>
        /// <returns></returns>

        public static QueryBookInfo[] FuzzySearch(string query, int start, int limit)
        {
            string host = "http://api.zhuishushenqi.com/book/fuzzy-search";
            var param_query = new KeyValuePair<string, string>("query", query);
            var param_start = new KeyValuePair<string, string>("start", start.ToString());
            var param_limit = new KeyValuePair<string, string>("limit", limit.ToString());
            var ret = HttpHelper.Get<FuzzySearchResult>(host, param_query, param_start, param_limit);

            if (ret.ok)
            {
                return ret.books.ToArray();
            }
            return Enumerable.Empty<QueryBookInfo>().ToArray();
        }

        /// <summary>
        /// 获得混合书源
        /// </summary>
        /// <param name="bookid"></param>
        /// <returns></returns>
        public static MixTocInfo GetMixToc(string bookid)
        {
            string host = string.Format("http://api.zhuishushenqi.com/mix-toc/{0}", bookid);
            var ret = HttpHelper.Get<MixTocResult>(host);

            if (ret.ok)
            {
                return ret.mixToc;
            }
            return null;
        }

        /// <summary>
        /// 获得书源
        /// </summary>
        /// <param name="bookid"></param>
        /// <returns></returns>
        public static TocSummmaryInfo[] GetTocSummary(string bookid)
        {
            string host = string.Format("http://api.zhuishushenqi.com/toc?view=summary&book=" + bookid);
            var infoList = HttpHelper.Get<List<TocSummmaryInfo>>(host);
            return infoList.Where(g => g.name != "优质书源").ToArray();
        }

        /// <summary>
        /// 获得章节列表
        /// </summary>
        /// <param name="tocid">书源ID</param>
        /// <returns></returns>

        public static TocChaperListInfo GetChaperList(string tocid)
        {
            string host = string.Format("http://api.zhuishushenqi.com/toc/{0}?view=chapters", tocid);
            var ret = HttpHelper.Get<TocChaperListInfo>(host);
            return ret;
        }
        /// <summary>
        /// 获得章节内容
        /// </summary>
        /// <param name="link">章节列表中的Link</param>
        /// <returns></returns>
        public static ChapterInfo GetChapter(string chaperLink)
        {
            int timestamp = ConvertDateTimeInt(DateTime.Now);
            chaperLink = System.Web.HttpUtility.UrlEncode(chaperLink, Encoding.UTF8);
            string host = string.Format("http://chapter2.zhuishushenqi.com/chapter/{0}?k=2124b73d7e2e1945&t={1}", chaperLink, timestamp);
            var ret = HttpHelper.Get<ChapterInfoResult>(host);
            if (ret.ok)
            {
                return ret.chapter;
            }
            return null;
        }

        public static string[] AutoComplate(string query)
        {
            string[] words = new string[0];
            string host = string.Format("http://api.zhuishushenqi.com/book/auto-complete?query={0}", query);
            var ret = HttpHelper.Get<AutoComplateResult>(host);

            if (ret.ok)
            {
                return ret.keywords.ToArray();
            }
            return null;
        }




        /// <summary>  
        /// DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time"> DateTime时间格式</param>  
        /// <returns>Unix时间戳格式</returns>  
        private static int ConvertDateTimeInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }
    }
}
